<template>
  <div class="assets">
    <div>
      <jc-button
        :btn-arr="['reload', 'voucher', 'save', 'sub', 'remove', 'pass', 'list']"
        :disab-arr="[isDisabled && 'save']"
        :list="'/disposeList'"
        @refresh="assetsDetails"
        @save="save"
        @submit="save(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="remove"
        @voucher="credentialsTask"
        @voucherLok="LooktialsTask"
      />
    </div>
    <el-tabs>
      <el-tab-pane :label="$t('market.info')">
        <el-form :model="proValue" label-width="100px" size="mini" :disabled="isDisabled">
          <el-form-item :label="$t('shop.state')">
            <el-tag :type="proValue.stateColor">{{ proValue.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('shop.fnumber')">
            <el-input v-model="proValue.fbillno" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.billType')">
            <el-select v-model="proValue.fbilltypeid">
              <el-option
                v-for="item in billtype1"
                :key="item.fid"
                :label="item.fname"
                :value="item.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.businessDate')" prop="fdate">
            <el-date-picker
              v-model.trim="proValue.fdate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 163px"
            />
          </el-form-item>
          <el-form-item :label="$t('market.disposalWay')">
            <el-select v-model="proValue.fmodeid">
              <el-option
                v-for="item in billtype"
                :key="item.fid"
                :label="item.fname"
                :value="item.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.disposalReason')" prop="freason">
            <el-input v-model="proValue.freason" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.assets')" name="first">
        <div style="margin-bottom: 5px">
          <el-button size="mini" type="primary" @click="newHang">{{ $t('btn.newHang') }}</el-button>
          <el-button size="mini" type="danger" @click="delHang">{{ $t('btn.delHang') }}</el-button>
        </div>
        <jc-table
          serial
          :table-data="tableData"
          :table-header="tableHeader"
          table-height="300px"
          @rowClick="rowClick"
        >
          <!--卡片编码-->
          <template v-slot:tagSlot="col">
            <el-input v-model="col.scope.row.fassetsbillno" size="mini" :disabled="isDisabled">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handelWorkshop(col.scope.$index)"
              />
            </el-input>
          </template>
          <!--处置数量-->
          <template v-slot:billSlot="col">
            <el-input-number v-model="col.scope.row.fmanageamount" size="mini" :disabled="isDisabled" />
          </template>
          <!--清理费用-->
          <template v-slot:btnState="col">
            <el-input-number v-model="col.scope.row.fprice" size="mini" :disabled="isDisabled" />
          </template>
          <!--残值收入-->
          <template v-slot:btnStates="col">
            <el-input-number v-model="col.scope.row.finprice" size="mini" :disabled="isDisabled" />
          </template>
        </jc-table>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.fince')" name="second">
        <div>
          <el-form :model="proValue1" label-width="100px" size="mini" disabled>
            <el-form-item :label="$t('market.cardNumber')" prop="fdate">
              <el-input v-model="proValue1.fassetsbillno" />
            </el-form-item>
            <el-form-item :label="$t('market.assetsname')" prop="fdate">
              <el-input v-model="proValue1.fassetsname" />
            </el-form-item>
          </el-form>
        </div>
        <div>
          <jc-table
            :table-data="tableData2"
            :table-header="tableHeader2"
            table-height="300px"
          />
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-tabs>
      <el-tab-pane :label="$t('market.entity')">
        <div style="margin-bottom: 5px">
          <el-button size="mini" type="danger" @click="delHang1">{{ $t('btn.delHang') }}</el-button>
        </div>
        <jc-table
          :table-data="tableData[indexSelf].list"
          :table-header="tableHeader3"
          table-height="300px"
          @rowClick="rowClick1"
        >
          <template v-slot:tagSlot="col">
            <el-input-number v-model="col.scope.row.famount" :max="col.scope.row.fassetsamount" size="mini" :disabled="isDisabled" />
          </template>
        </jc-table>
      </el-tab-pane>
    </el-tabs>
    <!--  卡片编码  -->
    <assets v-if="assetsVis" @assets="assetsData" />
  </div>
</template>

<script>
import {
  deleteAssetsManages,
  noreviewAssetsManage,
  nosubmitAssetsManage,
  queryAssetsDetails, queryAssetsManageDetails, reviewAssetsManage, submitAssetsManage, updateAssetsManage
} from '@/api/financial/assets'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'
import { createVoucherByManage } from '@/api/financial/cashier'
export default {
  components: {
    jcButton,
    assets: () => import('@/views/financial/assets/dispose/component/assets')
  },
  data() {
    return {
      assetsVis: false,
      isDisabled: false,
      activeName: 'first',
      proValue: {},
      indexSelf: 0,
      billtype: [], // 处置
      billtype1: [], // 处置
      tableData: [],
      tableHeader: [
        { label: this.$t('market.cardNumber'), type: 'tag', prop: 'fassetsbillno', requireg: 'must' },
        { label: this.$t('market.assetsname'), prop: 'fassetsname' },
        { label: this.$t('market.fqty'), prop: 'fassetsamount' },
        { label: this.$t('market.funit'), prop: 'funitname' },
        { label: this.$t('market.manageamount'), type: 'bill', prop: 'fmanageamount', width: '90', requireg: 'must' },
        { label: this.$t('market.managecurrency'), prop: 'fcurrencyname' },
        { label: this.$t('market.cleaningCost'), type: 'state', prop: 'fprice', width: '90', requireg: 'must' },
        { label: this.$t('market.salvage'), type: 'states', prop: 'finprice', width: '90', requireg: 'must' }
      ],
      matter: [], // 其他信息
      proValue1: {},
      tableData2: [],
      tableHeader2: [
        { label: this.$t('market.policy'), prop: 'fpolicyname' },
        { label: this.$t('market.currency2'), prop: 'fcurrencyname' },
        { label: this.$t('market.assetsoriginal'), prop: 'fassetsoriginalpricedeal' },
        { label: this.$t('market.cumulativedep'), prop: 'fcumulativedeppricedeal' },
        { label: this.$t('market.expectresidualf'), prop: 'fexpectresidualfpricedeal' }
      ],
      delArr: [],
      // 实物
      tableData3: [],
      tableHeader3: [
        { label: this.$t('market.assetsname'), prop: 'fassetsname' },
        { label: this.$t('market.barcode1'), prop: 'fbarcode' },
        { label: this.$t('market.specify'), prop: 'fmodel' },
        { label: this.$t('market.areaname'), prop: 'fareaname' },
        { label: this.$t('market.assetQty'), prop: 'fassetsamount' },
        { label: this.$t('market.manageamount'), prop: 'famount', type: 'tag' },
        { label: this.$t('market.assetsnum'), prop: 'fassetsbillno' },
        { label: this.$t('market.supplier'), prop: 'fsuppliername' }
      ]
    }
  },
  created() {
    this.queryTBasBilltype()
    this.queryTBasBilltype1()
    if (this.$route.params.id) {
      this.assetsDetails()
    }
  },
  methods: {
    assetsDetails() {
      const fid = this.$route.params.id
      queryAssetsManageDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.proValue = toMxBmina(res.data)
          this.tableData = res.data.details
          this.tableData.map(res => {
            res.delArr = []
          })
          this.handleGetUntreated(res.data.details[0])
          this.isDisabled = this.proValue.fdocumentstatus === this.$t('market.state1') || this.proValue.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    save(ev) {
      const DATA = {
        ...notoMxBmina(this.proValue),
        details: this.tableData,
        delArr: this.delArr
      }
      updateAssetsManage(DATA).then(res => {
        if (res.code === 0) {
          this.delArr = []
          ev === 1 ? this.submit(1) : this.assetsDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }, // 打开卡片编码
    handelWorkshop(index) {
      this.indexSelf = index
      this.assetsVis = true
    }, // 卡片编码
    assetsData(ev) {
      if (ev.fassetsamount <= 0) {
        this.assetsVis = false
        this.$message({ showClose: true, message: '资产数量必须大于0', type: 'warning' })
        return
      }
      const item = this.tableData[this.indexSelf]
      if (ev.fassetsbillno) {
        item.fassetsbillno = ev.fbillno
        item.fassetsname = ev.fassetsname
        item.fassetsamount = ev.fassetsamount
        item.funitname = ev.funitname
        item.fbillid = ev.fid
        item.fentryid = 0
        this.handleGetUntreated(item)
      }
      this.assetsVis = false
    },
    // 详情
    handleGetUntreated(ev) {
      const fid = ev.fbillid
      queryAssetsDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.proValue1 = toMxBmina(res.data)
          this.proValue1.fassetsbillno = this.proValue1.fbillno
          const { fpolicyname, fcurrencyname, fassetsoriginalpricedeal,
            fcumulativedeppricedeal, fexpectresidualfpricedeal, fid } = this.proValue1
          this.tableData2 = [{
            fpolicyname, fcurrencyname, fassetsoriginalpricedeal,
            fcumulativedeppricedeal, fexpectresidualfpricedeal, fid
          }]
          // this.tableData3 = this.proValue1.details
          this.proValue1.details.map(res => {
            res.fassetsname = this.proValue1.fassetsname
            res.fmanageentryid = res.fentryid
            res.fassetsamount = res.famount
          })
          const item = this.tableData[this.indexSelf]
          if (!item.fentryid || item.list.length === 0) {
            this.tableData[this.indexSelf].list = this.proValue1.details
          }
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 处置方式
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_MANAGE_MODE' })
      this.billtype = RES.list
    },
    // 资产处置
    async queryTBasBilltype1() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_MANAGE' })
      this.billtype1 = RES.list
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.proValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.assetsDetails()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitAssetsManage : nosubmitAssetsManage
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewAssetsManage : noreviewAssetsManage
      this.toExamineFun(api)
    },
    // 删除
    remove() {
      this.$confirm('确定要删除该订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const DATA = { Numbers: [this.proValue.fbillno] }
        deleteAssetsManages(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/disposeList' })
        })
      })
    },
    // 凭证生成
    credentialsTask() {
      const DATA = {
        fids: [this.proValue.fid]
      }
      createVoucherByManage(DATA).then(res => {
        if (res.code === 0) {
          this.$message({ showClose: true, message: res.message, type: 'success' })
          this.$router.push({ path: '/certificate' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 查看凭证
    LooktialsTask() {
      const id = this.proValue.voucherid
      if (id) {
        this.$router.push({ path: `/certificateDetail/${id}` })
      } else {
        this.$confirm('该单据未能查询到对应的业务凭证！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    newHang() {
      this.tableData.push({
        fassetsbillno: '',
        fassetsname: '',
        fassetsamount: 0,
        funitname: '',
        fmanageamount: 0,
        fcurrencyname: '人民币',
        fcurrencyid: 1,
        fprice: '',
        finprice: '',
        list: []
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
      if (row.fbillid) {
        this.handleGetUntreated(row)
      }
    },
    // 删除行
    delHang() {
      const item = this.tableData[this.indexSelf]
      this.delArr.push(item.fentryid)
      this.tableData.splice(this.indexSelf, 1)
    },
    // 实物点击行
    rowClick1(row) {
      this.indexSelf1 = row.index
    },
    // 实物删除行
    delHang1() {
      const item = this.tableData[this.indexSelf]
      item.delArr.push(item.list[this.indexSelf1].fmanagematerialid)
      const fnum = item.list[this.indexSelf1].famount // 删除的实物数量
      item.fmanageamount = item.fmanageamount - fnum // 资产处置数量 - 实物数量
      this.tableData[this.indexSelf].list.splice(this.indexSelf1, 1)
    }
  }
}
</script>

<style scoped lang="scss">
.assets {
  @include listBom;
  .title {
    font-size: 14px;
    margin-top: 5px;
    color: #1e9fff;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 275px;
      margin-bottom: 5px;
    }
  }
  .el-table{
    &::v-deep .el-input-number .el-input__inner{
      padding: 0 5px;
      width: 80px;
    }
    &::v-deep .el-input-number .el-input{
      width: 80px;
    }
    &::v-deep .el-input-number--mini{
      width: 80px;
    }
    &::v-deep .el-input-number__increase{
      display: none;
    }
    &::v-deep .el-input-number__decrease{
      display: none;
    }
    &::v-deep .el-table__footer-wrapper tbody td{
      background: #fdf6ec!important;
    }
  }
}
</style>

